package com.bup.gn.controller;

import com.bup.gn.model.User;
import com.bup.gn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RegisterController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/register.html")
    public String show() {
        return "register.html";
    }

    @RequestMapping(value = "/process-register.html")
    public String register(User user, Model model) {
        boolean emailExisted = userService.checkEmail(user.getEmail());
        if (emailExisted) {
            model.addAttribute("emailExisted", emailExisted);
        }
        if (emailExisted) {
            return "register.html";
        }
        userService.createAccount(user);
        return "register-confirmation.html";
    }
}